﻿/*
 * Copyright(C) 2012,ajayumi 保留所有权利。( All rights reserved. )
 * 
 * 文件名称：StyleHelper.cs
 * 摘    要：
 * 当前版本：1.0
 * 作    者：黄乙冬 (ajayumi)
 * 创建日期：2012年5月24日
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace ajayumi.Platform.Web
{
    /// <summary>
    /// StyleHelper.cs 样式辅助类
    /// Author   : 黄乙冬
    /// Date     : 2011-12-01
    /// </summary>
    public class StyleHelper : IDisposable
    {
        #region Variables

        #endregion

        #region Properites

        /// <summary>
        /// 样式文件的完整路径
        /// </summary>
        public string Path { get; private set; }

        /// <summary>
        /// 样式文件的内容
        /// </summary>
        public string CssText { get; private set; }

        #endregion

        /// <summary>
        /// 样式表文件的完整路径
        /// </summary>
        /// <param name="path"></param>
        public StyleHelper(string path)
        {
            //this.Path = path;
            //this.Load();
            this.Load(path);
        }

        ///// <summary>
        ///// 载入样式文件
        ///// </summary>
        //private void Load()
        //{
        //    this.Load(this.Path);
        //}

        /// <summary>
        /// 载入样式文件
        /// </summary>
        /// <param name="path"></param>
        public void Load(string path)
        {
            this.Path = path;
            try
            {
                using (StreamReader sr = new StreamReader(path, Encoding.UTF8))
                {
                    this.CssText = sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 保存样式文件
        /// </summary>
        public void Save()
        {
            this.Save(this.CssText);
        }

        /// <summary>
        /// 保存样式文件
        /// <param name="cssText">样式文本字符串</param>
        /// </summary>
        public void Save(string cssText)
        {
            using (StreamWriter sw = new StreamWriter(this.Path, false, Encoding.UTF8))
            {
                sw.Write(cssText);
            }
        }

        /// <summary>
        /// 读取样式，不区分大小写
        /// </summary>
        /// <param name="selector">选择器</param>
        /// <returns></returns>
        public string Read(string selector)
        {
            return this.Read(selector, string.Empty);
        }
        /// <summary>
        /// 读取样式，不区分大小写
        /// </summary>
        /// <param name="selector">选择器</param>
        /// <param name="attribute">属性</param>
        /// <returns></returns>
        public string Read(string selector, string attribute)
        {
            string cssStr = string.Empty;
            try
            {
                string pattern = string.Format("{0}.[^}}]+}}", selector);
                cssStr = Regex.Match(this.CssText, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Value;
                if (!string.IsNullOrEmpty(attribute))
                {
                    pattern = string.Format("{0}.[^;]+;", attribute);
                    cssStr = Regex.Match(cssStr, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline).Value;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return cssStr;
        }

        /// <summary>
        /// 写入样式，区分大小写
        /// <param name="selector">选择器</param>
        /// <param name="value">写入的样式</param>
        /// </summary>
        public void Write(string selector, string value)
        {
            this.Write(selector, string.Empty, value);
        }

        /// <summary>
        /// 写入样式，区分大小写
        /// </summary>
        /// <param name="selector">选择器</param>
        /// <param name="attribute">属性</param>
        /// <param name="value">写入的样式</param>
        public void Write(string selector, string attribute, string value)
        {
            string oldCssStr = string.Empty;
            string newCssStr = value;
            try
            {
                string pattern = string.Format("{0}.[^}}]+}}", selector);
                oldCssStr = Regex.Match(this.CssText, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Value;
                //if (!string.IsNullOrEmpty(attribute))
                //{
                //    pattern = string.Format("{0}.[^;]+;", attribute);
                //    Match match = Regex.Match(oldCssStr, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                //    if (match.Success)
                //    { }
                //    else
                //    { }
                //}
                this.CssText = this.CssText.Replace(oldCssStr, newCssStr);
                this.Save();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Dispose()
        {

        }
    }
}
